package trocabi.model;

//Préfixes des variables et méthodes
import java.util.Date;
import trocabi.Socle.DBManager;
import trocabi.Socle.StringTools;
/**
 *
 * @author philippe
 */
public class Article implements Cloneable {
    public final static Object aspect[][]={
        {(Object) "Neuf",            (Object) "N",(Object) trocabi.Parameters.MARGE_NEUF},
        {(Object) "Gros article",    (Object) "G",(Object) trocabi.Parameters.MARGE_ANCIEN},
        {(Object) "Petit article",   (Object) "p",(Object) trocabi.Parameters.MARGE_ANCIEN}}; // TODO: set marge
    public final static String genre[][]={{"Homme","H"},{"Femme","F"},{"Enfant","E"}};
    public final static String type[][]={{"Vêtement","V"},{"Chaussure","C"},{"Accessoire","A"},{"Autre","O"}};
    public final static String colors[]={"Blanc / Beige","Bleu","Jaune","Noir","Rose","Rouge","Vert","Violet"};
    
    public enum Etat { DEPOT, EN_RECHERCHE, RETROUVE, VENDU, BRADERIE, RENDU } 
    private long    id;
    private long    idDepositaire;
    private String  description; //Maximum 255 caractère
    private String  codeAspect;    //Maximum 13 caractères
    private String  codeType; //Maximum 10 caractères
    private String  codeGenre;   //Maximum 6 caractères
    private String  couleur;  //Maximum 13 caractères
    private Etat    etat; 
    private String  commentaire; //Maximum 255 caractères
    private String  codeCaisse;  //Maximum 2 caractères TODO ????
    private double  prixAchat;
    private double  marge;
    private Date    dateDepot; 
    private Date    datePaiment; 
    private Date    dateAchat;
    private Date    dateDernierEtat;  

    //Doit être inférieur strictement à 10000000 (longueur inférieur strictement à 8 chiffres
    public static String aspectToCode(String aspectName) {
        for (int i=0; i<aspect.length; i++) {
            if (aspectName.compareTo((String) aspect[i][0])==0) return (String) aspect[i][1];
        }
        return "";
    } 
    public static String codeToAspect(String code) {
         for (int i=0; i<aspect.length; i++) {
            if (code.compareTo((String) aspect[i][1])==0) return (String) aspect[i][0];
        }
        return "";
    }
    
    public static double codeToMarge(String code) {
         for (int i=0; i<aspect.length; i++) {
            if (code.compareTo((String) aspect[i][1])==0) return (double) aspect[i][2];
        }
        return 0;
    }
    
    public static String genreToCode(String genreName) {
        for (int i=0; i<genre.length; i++) {
            if (genreName.compareTo(genre[i][0])==0) return genre[i][1];
        }
        return "";
    }  
    public static String codeToGenre(String code) {
         for (int i=0; i<genre.length; i++) {
            if (code.compareTo(genre[i][1])==0) return genre[i][0];
        }
        return "";
    }
    public static String typeToCode(String typeName) {
        for (int i=0; i<type.length; i++) {
            if (typeName.compareTo(type[i][0])==0) return type[i][1];
        }
        return "";
    }  
    public static String codeToType(String code) {
         for (int i=0; i<type.length; i++) {
            if (code.compareTo(type[i][1])==0) return type[i][0];
        }
        return "";
    }
    
    public static String codeToColor(int pos) {
        return colors[pos];
    }
   
    public static String etatToString(Etat etat) {
        switch(etat) {
            case DEPOT:     return "DEPOT";
            case RETROUVE:  return "RETROUVÉ";
            case VENDU:     return "VENDU";
            case BRADERIE:  return "BRADERIE";
            case RENDU:     return "RENDU";
        }
        return "?";
    }
    
    public static String etatToCode(Etat etat) {
        switch(etat) {
            case DEPOT:     return "D";
            case RETROUVE:  return "R";
            case VENDU:     return "V";
            case BRADERIE:  return "B";
            case RENDU:     return "U";
        }
        return "?";
    }
    
     public static Etat codeToEtat(String code) {
        switch(code) {
            case "D": return Etat.DEPOT;
            case "R": return Etat.RETROUVE;
            case "V": return Etat.VENDU;
            case "B": return Etat.BRADERIE;
            case "U": return Etat.RENDU;
        }
        return Etat.DEPOT;
    }
   
    public void setId(Long id) {
        this.id=id;
    }

    public long getId() {
        return id;
    }

    public long getIdDepositaire() {
        return idDepositaire;
    }

    public String getDescription() {
        return description;
    }

    public String getCodeAspect() {
        return codeAspect;
    }

    public String getCodeType() {
        return codeType;
    }

    public String getCodeGenre() {
        return codeGenre;
    }

    public String getCouleur() {
        return couleur;
    }

    public Etat getSituation() {
        return etat; 
    }
    public void setSituation(Etat etat) {
        this.etat=etat;
    }
    
    public Date getDateChangementEtat() {
        return dateDernierEtat;
    }
    public void setDateChangementEtat(Date date) {
        dateDernierEtat=DBManager.dateToSqlDate(date);
    }
    public String getCommentaire() {
        return commentaire;
    }

    public String getCodeCaisse() {
        return codeCaisse;
    }

    public Date getDateDepot() {
        return dateDepot;
    }

    public double getPrixAchat() {
        return prixAchat;
    }

    public double getTxMarge() {
        return marge;
    }

  
    public Date getDatePaiementDepositaire() { // TODO: ajouter colonne
        return datePaiment;
    }
    
    public void setDatePaiementDepositaire(Date date) {
        datePaiment=date;
    }
    
    public Date getDateAchat() {
        return dateAchat;
    }
    
    public void setDateAchat(Date _dateAchat) {
        dateAchat=_dateAchat;
    }
     /*
     * Use it to insert new depositaire in database
     */
     public Article(long _idDepositaire,  
                    Date _dateDepot, String _description,  
                    String _codeAspect, String _codeGenre, String _codeType, 
                    String _couleur, double _prixAchat) {
        id=-1; // no id, just for creation
        idDepositaire = _idDepositaire; 
        description = _description;
        codeAspect = _codeAspect;
        codeType = _codeType;
        codeGenre = _codeGenre;
        couleur = _couleur;
        etat = Etat.DEPOT;
        commentaire = "";
        dateDepot = _dateDepot;
        codeCaisse = null;  
        prixAchat = _prixAchat; 
        marge = codeToMarge(codeAspect);
        datePaiment=null; 
        dateAchat=null;
        dateDernierEtat=new Date();
    }
     
     public Article(long _id, long _idDepositaire,  String _description,
                    String _codeAspect, String _codeGenre, String _codeType, 
                    String _couleur, Etat _etat, String _commentaire, String _codeCaisse,
                    double _prixAchat, double _marge,
                    Date _dateDepot, Date _datePaiement, 
                    Date _dateAchat, Date _dateDernierEtat) {
      id=_id;
      idDepositaire=_idDepositaire;
      description=_description;
      codeAspect=_codeAspect;
      codeType=_codeType; 
      codeGenre=_codeGenre; 
      couleur=_couleur; 
      etat=_etat; 
      commentaire=_commentaire; 
      codeCaisse=_codeCaisse;
      prixAchat=_prixAchat;
      marge=_marge;
      dateDepot=_dateDepot; 
      datePaiment=_datePaiement; 
      dateAchat=_dateAchat; 
      dateDernierEtat=_dateDernierEtat;
     }
     
    public Object clone() {
        Article article = null;
        try {
            // On récupère l'instance à renvoyer par l'appel de la méthode super.clone()
            article = (Article) super.clone();
        } catch (CloneNotSupportedException cnse) {
            // Ne devrait jamais arriver car nous implémentons 
            // l'interface Cloneable
            cnse.printStackTrace(System.err);
        }
     
        return article;    // on renvoie le clone
    }
}
